package com.gzmu.storage.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gzmu.storage.entity.Cabinet;
import com.gzmu.storage.entity.Chest;
import com.gzmu.storage.entity.Record;
import com.gzmu.storage.mapper.ChestMapper;
import com.gzmu.storage.mapper.RecordMapper;
import com.gzmu.storage.service.AdminlogService;
import com.gzmu.storage.service.CabinetService;
import com.gzmu.storage.service.RecordService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaobo Yan
 * @since 2020-04-16
 */
@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private ChestMapper chestMapper;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private AdminlogService adminlogService;


    @ResponseBody
    @RequestMapping("addRecord")
    public Object addRecord(Record record){
        Map map = new HashMap<>();
        Date date = new Date(  );
        Cabinet cabinet = cabinetService.selectById( record.getCabinetid() );
        Chest chest = chestMapper.selectById( record.getChestid() );
        record.setCreatetime( date );
        record.setUpdatetime( date );
        record.setCabinetname( cabinet.getName() );
        record.setChestname( chest.getName() );
        record.setStatus( 1 );
        record.setRemark( "档案" );
        boolean b = recordService.insertAndUpdate( record );
        b &= chestMapper.updateChestAddByID(record.getChestid());
        adminlogService.insertAdminlog("归档" + record.getName() + "档案");
        map.put( "code", b);
        return JSONObject.toJSON(map);
    }

    @ResponseBody
    @RequestMapping("getRecord")
    public Object getRecord(@RequestParam Map<String, String> params,int page, int limit){
        Map map = new HashMap<>();
        params.remove( "page" );
        params.remove( "limit" );
        EntityWrapper wrapper = new EntityWrapper(  );
        wrapper.allEq( params );
        RowBounds rowBounds = new RowBounds( (page - 1) * limit, limit );
        List<Record> records = recordMapper.selectPage( rowBounds, wrapper);
//        List<Record> records = recordMapperVo.getRecordVos( new Page<>( (page - 1) * limit, limit ), wrapper );
        int count = recordMapper.selectCount( wrapper );
        map.put( "code", 0);
        map.put( "count", count);
        map.put( "data", records );
        return JSONObject.toJSON(map);
    }

    @ResponseBody
    @RequestMapping("reloadRecord")
    public Object reloadRecord(String searchWay, String keyWord, int page, int limit){
        Map map = new HashMap<>();
        EntityWrapper wrapper = new EntityWrapper(  );
        wrapper.eq( searchWay, keyWord );
        RowBounds rowBounds = new RowBounds( (page - 1) * limit, limit );
        List<Record> records = recordMapper.selectPage( rowBounds, wrapper);
        int count = recordMapper.selectCount( wrapper );
        map.put( "code", 0);
        map.put( "count", count);
        map.put( "data", records );
        return JSONObject.toJSON(map);
    }

    @ResponseBody
    @RequestMapping("recordBorrow")
    public Object recordBorrow(Integer id, String chestid, String name){
        Map map = new HashMap<>();
        boolean b = recordMapper.updateRecodStatus( id, 0 );
        b &= chestMapper.updateChestSubByID( chestid );
        adminlogService.insertAdminlog("借阅" + name + "档案");
        map.put( "code", b);
        return JSONObject.toJSON(map);
    }
}

